Write-Host "正在启动HLS视频流播放器..." -ForegroundColor Green
Write-Host ""

# 检查Node.js是否安装
try {
    $nodeVersion = node --version
    Write-Host "✓ Node.js版本: $nodeVersion" -ForegroundColor Green
} catch {
    Write-Host "✗ 未找到Node.js，请先安装Node.js" -ForegroundColor Red
    Write-Host "下载地址: https://nodejs.org/" -ForegroundColor Yellow
    Read-Host "按任意键退出"
    exit 1
}

# 检查FFmpeg是否安装
try {
    $ffmpegVersion = ffmpeg -version | Select-Object -First 1
    Write-Host "✓ FFmpeg已安装: $ffmpegVersion" -ForegroundColor Green
} catch {
    Write-Host "⚠ 警告: 未找到FFmpeg，视频转换功能将无法使用" -ForegroundColor Yellow
    Write-Host "请安装FFmpeg并添加到PATH环境变量" -ForegroundColor Yellow
    Write-Host "下载地址: https://ffmpeg.org/download.html" -ForegroundColor Yellow
    Write-Host ""
}

Write-Host ""

# 1. 安装后端依赖
Write-Host "1. 安装后端依赖..." -ForegroundColor Cyan
npm install
if ($LASTEXITCODE -ne 0) {
    Write-Host "✗ 后端依赖安装失败！" -ForegroundColor Red
    Read-Host "按任意键退出"
    exit 1
}

Write-Host ""

# 2. 安装前端依赖
Write-Host "2. 安装前端依赖..." -ForegroundColor Cyan
npm run install:client
if ($LASTEXITCODE -ne 0) {
    Write-Host "✗ 前端依赖安装失败！" -ForegroundColor Red
    Read-Host "按任意键退出"
    exit 1
}

Write-Host ""

# 3. 启动后端服务器
Write-Host "3. 启动后端服务器..." -ForegroundColor Cyan
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$PWD'; npm start" -WindowStyle Normal

Write-Host ""

# 4. 等待后端启动
Write-Host "4. 等待后端启动完成..." -ForegroundColor Cyan
Start-Sleep -Seconds 5

Write-Host ""

# 5. 启动前端开发服务器
Write-Host "5. 启动前端开发服务器..." -ForegroundColor Cyan
Start-Process powershell -ArgumentList "-NoExit", "-Command", "cd '$PWD'; npm run dev:client" -WindowStyle Normal

Write-Host ""
Write-Host "🎉 启动完成！" -ForegroundColor Green
Write-Host ""
Write-Host "访问地址:" -ForegroundColor Yellow
Write-Host "  后端API: http://localhost:3000" -ForegroundColor White
Write-Host "  前端页面: http://localhost:5173" -ForegroundColor White
Write-Host ""
Write-Host "使用说明:" -ForegroundColor Yellow
Write-Host "  1. 在首页配置RTSP地址" -ForegroundColor White
Write-Host "  2. 点击'启动流'开始转换" -ForegroundColor White
Write-Host "  3. 点击'观看视频流'进入播放器" -ForegroundColor White
Write-Host ""
Write-Host "如需停止服务，请关闭对应的PowerShell窗口" -ForegroundColor Cyan
Write-Host ""
Read-Host "按任意键退出"
